package views;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.DbHelper;
import presenters.RentChgReqPresenter;


public class RentChgReqPanel extends JPanel {
	private JTextField textField;
	private JTextField textFieldUser;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private JTextField textField_7;
	private JTextField textField_1;
	private JTextField textField_2;
	private JButton btnShowCarAvailability;
	private JButton btnCancelReservation;
	private JButton updateButton;
	private JComboBox comboBox; // model
	private JComboBox comboBox_1; // location
	private RentChgReqPresenter myPresenter;

	/**
	 * Create the panel.
	 */
	public RentChgReqPanel() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.CENTER);
		
		Box verticalBox = Box.createVerticalBox();
		panel.add(verticalBox);
		DbHelper myHelper = new DbHelper();
		
		JLabel lblUsername = new JLabel("Username:");
		verticalBox.add(lblUsername);
		
		textFieldUser = new JTextField();
		verticalBox.add(textFieldUser);
		textFieldUser.setColumns(10);
		
		Component verticalStrut = Box.createVerticalStrut(20);
		verticalBox.add(verticalStrut);
		
		JLabel lblCarModel = new JLabel("Car Model:");
		verticalBox.add(lblCarModel);
		
		comboBox = new JComboBox(myHelper.getCarModels());
		verticalBox.add(comboBox);
		
		JLabel lblCarLocation = new JLabel("Car Location:");
		verticalBox.add(lblCarLocation);
		
		comboBox_1 = new JComboBox(myHelper.getLocations());
		verticalBox.add(comboBox_1);
		
		JLabel lblPickupTime = new JLabel("Original Arrival Time [\"month\", \"day\", \"year\", \"hour\", \"minute\"]:");
		verticalBox.add(lblPickupTime);
		
		textField = new JTextField();
		verticalBox.add(textField);
		textField.setColumns(10);
		
		JLabel lblReturnTime = new JLabel("Now Arrival Time [\"month\", \"day\", \"year\", \"hour\", \"minute\"]:");
		verticalBox.add(lblReturnTime);
		
		textField_4 = new JTextField();
		verticalBox.add(textField_4);
		textField_4.setColumns(10);
		
		updateButton = new JButton("Update");
		verticalBox.add(updateButton);
		updateButton.addActionListener(new MyActionListener());
		
		Box verticalBox_1 = Box.createVerticalBox();
		add(verticalBox_1, BorderLayout.EAST);
		
		JLabel lblUserAff = new JLabel("User Affected:");
		verticalBox_1.add(lblUserAff);
		
		textField_5 = new JTextField();
		verticalBox_1.add(textField_5);
		textField_5.setColumns(10);
		
		JLabel lblPickupTime2 = new JLabel("Original Pick-up Time [\"month\", \"day\", \"year\", \"hour\", \"minute\"]:");
		verticalBox_1.add(lblPickupTime2);
		
		textField_6 = new JTextField();
		verticalBox_1.add(textField_6);
		textField_6.setColumns(10);
		
		JLabel lblReturnTime2 = new JLabel("Original Return Time [\"month\", \"day\", \"year\", \"hour\", \"minute\"]:");
		verticalBox_1.add(lblReturnTime2);
		
		textField_7 = new JTextField();
		verticalBox_1.add(textField_7);
		textField_7.setColumns(10);
		
		JLabel lblEmailAddress = new JLabel("Email Address:");
		verticalBox_1.add(lblEmailAddress);
		
		textField_1 = new JTextField();
		verticalBox_1.add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblPhoneNumber = new JLabel("Phone Number:");
		verticalBox_1.add(lblPhoneNumber);
		
		textField_2 = new JTextField();
		verticalBox_1.add(textField_2);
		textField_2.setColumns(10);
		
		btnShowCarAvailability = new JButton("Show Car Availability");
		verticalBox_1.add(btnShowCarAvailability);
		btnShowCarAvailability.addActionListener(new MyActionListener());
		
		btnCancelReservation = new JButton("Cancel Reservation");
		verticalBox_1.add(btnCancelReservation);
		btnCancelReservation.addActionListener(new MyActionListener());
		
		myPresenter = new RentChgReqPresenter(this);
	}
	
	public void populateResults(ResultSet rs) {
		String usersAffected = "";
		String original;
		try {
			rs.first();
			
			while(rs.next()) {
				
			}
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	private class MyActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			Object source = e.getSource();
			
			if(source.equals(btnCancelReservation)) {
				
			}
			else if (source.equals(btnShowCarAvailability)) {
				
			} else if(source.equals(updateButton)) {
				String username = textFieldUser.getText();
				String model = comboBox.getName();
				String location = comboBox_1.getName();
				String oldArrival = textField.getText();
				String newArrival = textField_4.getText();
				
				myPresenter.process(username, model, location, oldArrival, newArrival);
				
			}
			
		}
		
	}
}
